/*
 * Copyright (c) ycq_work@163.com
 */

package com.mxg.blog.controller.articleAdmin;

import com.mxg.blog.common.DetermineLogin;
import com.mxg.blog.common.ResultCode;
import com.mxg.blog.entity.Article;
import com.mxg.blog.service.ArticleService;
import com.mxg.blog.service.UserService;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author: ycq_work@163.com
 * @DATE: 2023/5/23 16:37
 * 逻辑删除文章
 */
@Controller
public class DeleteArticle {

    private final ArticleService articleService;
    private final UserService userService;

    public DeleteArticle(ArticleService articleService,
                             UserService userService)
    {
        this.articleService = articleService;
        this.userService = userService;
    }
    @PostMapping("api/deleteArticle")
    public ResponseEntity<Map<String, Object>> getAvatar(@RequestBody Map<String, String> requestMap) {
        Map<String, Object> resultMap = new HashMap<>();

        DetermineLogin determineLogin = DetermineLogin.getInstance(requestMap.get("userAccount"), userService);

        // 验证是否登录, 并获取user信息
        if(determineLogin.startJudge().equals(DetermineLogin.NOT_LOGIN)){
            resultMap.put(ResultCode.code, ResultCode.WRONG_CODE_NOT_LOGIN);
            resultMap.put(ResultCode.message, ResultCode.WRONG_MESSAGE_NOT_LOGIN);
            return ResponseEntity.ok(resultMap);
        }

        // 获取文章id
        Integer articleId;
        try{
            articleId = new Integer(requestMap.get("articleId"));
        }catch (Exception e)
        {
            resultMap.put(ResultCode.code, ResultCode.WRONG_CODE_WRONG_EDIT_ARTICLE);
            resultMap.put(ResultCode.message, ResultCode.WRONG_MESSAGE_WRONG_EDIT_ARTICLE);
            return ResponseEntity.ok(resultMap);
        }

        // 查询文章是否存在
        Article article = articleService.getArticleByArticleId(articleId);
        if(article != null){
            resultMap.put(ResultCode.code, ResultCode.SUCCESS_CODE);
            // 不存在 逻辑删除文章
            articleService.deleteArticleStatus0(articleId);
            resultMap.put(ResultCode.message, ResultCode.SUCCESS_MESSAGE);
        }

        return ResponseEntity.ok(resultMap);
    }
}
